home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Trial / Paint Shop Pro XI / Data1.cab / _8F70C045B1F04513ABAB5D93F94A48F5 < prev    next >
Encoding:
Text File  |  2006-08-04  |  13.8 KB  |  336 lines

  1. from PSPApp import *
  2. import PSPUtils
  3.  
  4. def ScriptProperties():
  5.     return {
  6.         'Author': u'Corel Corporation',
  7.         'Copyright': u'Copyright (c) 2002-2006 Corel Corporation. All rights reserved.',
  8.         'Description': "Place a caption on an image by expanding the canvas and placing the caption below the image.",
  9.         'Host': u'Paint Shop Pro 9',
  10.         'Host Version': u'9.00'
  11.         }
  12.  
  13. FontName = PSPUtils.CaptionFontName2        # font name that will be used by the text
  14. ExpandEdges = 0.1                # expand top, left and right by 10%
  15. ExpandBottom = 0.15                # expand bottom by 15%
  16.  
  17. def Do(Environment):
  18.     ''' Add a caption by doing the following steps:
  19.         (Thanks to Angela Cable for advice on artistic aspect of this)
  20.          1. Prompt the user for the caption.  If they press cancel abort
  21.          2. Select the bottommost layer
  22.          3. If we are starting on a background layer, promote it to a full layer. To keep
  23.             things straight,rename the layer to 'Image'
  24.          4. Add a new empty layer.  Call it Page Surface.
  25.          5. Arrange the Page Surface to the bottom
  26.          6. Increase the canvas size by 10% on the left, right and top,
  27.             and by 15% on the bottom.
  28.          7. Move the Page Surface layer into a layer group.  Call the group Album Page
  29.          8. Select the Page Surface layer
  30.          9. Flood fill it with white.
  31.         10. Using the backdrop texture and a 192 grey, flood fill it again.
  32.         11. Select the image layer again.
  33.         12. Add drop shadow on a new layer.
  34.         13. Arrange the layer into the Album page group
  35.         14. Create a vector layer
  36.         15. Place the caption text the user entered at the beginning on the image.
  37.             Font used is set in the FontName variable
  38.         16. Select the image layer again
  39.  
  40.        When done, the original layer is unchanged, and all of the new layers are placed
  41.        inside of a layer group.  The canvas size is changed however.
  42.     '''
  43.  
  44.     if PSPUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
  45.         return
  46.     
  47.     # save off the target doc before we do anything
  48.     Target = App.TargetDocument
  49.     
  50.     # we work with percentages for a number of things, and the expectation is that the
  51.     # text should be visible - lets not support any image with a dimension less than 200
  52.     if Target.Height < 200 or Target.Width < 200:
  53.         App.Do( Environment, 'MsgBox', {
  54.                 'Buttons': App.Constants.MsgButtons.OK, 
  55.                 'Icon': App.Constants.MsgIcons.Stop, 
  56.                 'Text': PSPUtils.ImageTooSmallMsg,
  57.                 })
  58.         return
  59.  
  60.     # if we are running on something that is not a flat image, suggest
  61.     # that they flatten before continuing
  62.     if PSPUtils.GetLayerCount( Environment, Target ) != 1:
  63.         result = App.Do( Environment, 'MsgBox', {
  64.                 'Buttons': App.Constants.MsgButtons.YesNo, 
  65.                 'Icon': App.Constants.MsgIcons.Question, 
  66.                 'Text': PSPUtils.MultipleLayersMsg 
  67.                 })
  68.         if result == 1: # yes
  69.             App.Do( Environment, 'LayerMergeAll' )
  70.     
  71.             
  72.     DefaultCaption = Target.Title
  73.     ExtensionPos = DefaultCaption.rfind( '.' )
  74.     if ExtensionPos != -1:
  75.         DefaultCaption = DefaultCaption[ : ExtensionPos ]
  76.     
  77.     # prompt for the caption at the very beginning - if they press cancel in the dialog we
  78.     # can return before any changes are made to the image
  79.     # now prompt the user for the caption to user
  80.     Result = App.Do( Environment, 'GetString', {
  81.             'DefaultText': DefaultCaption,
  82.             'DialogTitle': PSPUtils.CaptionTitle,
  83.             'Prompt': PSPUtils.CaptionPrompt,
  84.             'MaxLength': 40,
  85.             'GeneralSettings': {
  86.                 'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
  87.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  88.                 }
  89.         })
  90.     if Result[ 'OKButton' ] == App.Constants.Boolean.false:
  91.         return
  92.     
  93.     CaptionText = Result[ 'EnteredText' ]   # save this for later
  94.   
  95.     # go to the bottommost layer - this could throw an exception if we are already on the bottommost layer
  96.     App.Do( Environment, 'SelectLayer', {
  97.             'Path': ( 9999, -9999, [], App.Constants.Boolean.false),
  98.             'GeneralSettings': {
  99.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  100.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  101.                 }
  102.             })
  103.     
  104.     # promote a background layer so that we can put a drop shadow on it
  105.     if PSPUtils.LayerIsBackground( Environment, Target ):
  106.         App.Do( Environment, 'LayerPromoteBackground', {
  107.                 'GeneralSettings': {
  108.                     'ExecutionMode': App.Constants.ExecutionMode.Default, 
  109.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  110.                     }
  111.                 })
  112.  
  113.         # promoting the background will give it a default name - give it a
  114.         # modestly more descriptive name so that we can tell what is what
  115.         App.Do( Environment, 'LayerProperties', {
  116.                 'General': {
  117.                     'Name': PSPUtils.PromotedLayerName, 
  118.                     }, 
  119.                 'GeneralSettings': {
  120.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  121.                     'AutoActionMode': App.Constants.AutoActionMode.Default
  122.                     }
  123.                 })
  124.  
  125.     # now add a new layer
  126.     App.Do( Environment, 'NewRasterLayer', {
  127.             'General': {
  128.                 'Opacity': 100, 
  129.                 'Name': PSPUtils.PageSurfaceLayerName, 
  130.                 'IsVisible': App.Constants.Boolean.true, 
  131.                 }, 
  132.             'GeneralSettings': {
  133.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  134.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  135.                 }
  136.             })
  137.  
  138.     # arrange the new layer to the bottom
  139.     App.Do( Environment, 'LayerArrange', {
  140.             'Path': (0,-1,[],App.Constants.Boolean.false), 
  141.             'MoveAboveSibling': App.Constants.Boolean.false, 
  142.             'GeneralSettings': {
  143.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  144.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  145.                 }
  146.             })
  147.  
  148.     # by the time we are done we are going to have added 3 layers - backdrop, text, and shadow.
  149.     # to be tidy, lets wrap all of it in a layer group
  150.     # create a layer group.  This will place the 'page surface' layer in the group
  151.     App.Do( Environment, 'NewLayerGroup', {
  152.             'General': {
  153.                 'Name': PSPUtils.AlbumPageLayerGroup, 
  154.                 }, 
  155.             'GeneralSettings': {
  156.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  157.                 }
  158.             })
  159.     
  160.     # select the 'page surface' layer so that the vector layer gets created above it
  161.     App.Do( Environment, 'SelectLayer', {
  162.             'Path': (0,0,[1],App.Constants.Boolean.false), 
  163.             })
  164.  
  165.     # now increase the canvas size, based on the original size
  166.     XAmount = int(Target.Width * ExpandEdges)
  167.     YAmountTop = int(Target.Height * ExpandEdges)
  168.     YAmountBottom = int(Target.Height * ExpandBottom)
  169.     
  170.     NewWidth = Target.Width + 2 * XAmount
  171.     NewHeight = Target.Height + YAmountTop + YAmountBottom
  172.  
  173.     App.Do( Environment, 'ResizeCanvas', {
  174.             'HoriPlace': App.Constants.HorizontalType.Custom, 
  175.             'MaintainAspect': App.Constants.Boolean.false, 
  176.             'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
  177.             'NewHeight': NewHeight, 
  178.             'NewWidth': NewWidth, 
  179.             'OrigDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
  180.             'PlaceBottom': YAmountBottom, 
  181.             'PlaceLeft': XAmount, 
  182.             'PlaceRight': XAmount, 
  183.             'PlaceTop': YAmountTop, 
  184.             'VertPlace': App.Constants.VerticalType.Center, 
  185.             'GeneralSettings': {
  186.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  187.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  188.                 }
  189.             })
  190.     # we are still on the Backdrop layer - fill it with white
  191.     App.Do( Environment, 'Fill', {
  192.             'BlendMode': 0, 
  193.             'MatchMode': 1, 
  194.             'Material': {
  195.                 'Color': (255,255,255), 
  196.                 'Pattern': None, 
  197.                 'Gradient': None, 
  198.                 'Texture': None
  199.                 }, 
  200.             'UseForground': App.Constants.Boolean.true, 
  201.             'Opacity': 100, 
  202.             'Point': (1, 1), 
  203.             'SampleMerged': 0, 
  204.             'Tolerance': 20, 
  205.             'GeneralSettings': {
  206.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  207.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  208.                 }
  209.             })
  210.     
  211.     # now fill it again with a light grey texture
  212.     App.Do( Environment, 'Fill', {
  213.             'BlendMode': 0, 
  214.             'MatchMode': 1, 
  215.             'Material': {
  216.                 'Color': (160,160,160), 
  217.                 'Pattern': None, 
  218.                 'Gradient': None, 
  219.                 'Texture': {
  220.                     'Name': 'Corel_15_021', 
  221.                     'Image': None, 
  222.                     'Angle': 0.000, 
  223.                     'Scale': 100.000
  224.                     }
  225.                 }, 
  226.             'UseForground': App.Constants.Boolean.true, 
  227.             'Opacity': 100, 
  228.             'Point': (1, 1), 
  229.             'SampleMerged': 0, 
  230.             'Tolerance': 20, 
  231.             'GeneralSettings': {
  232.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  233.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  234.                 }
  235.             })
  236.  
  237.     # now select the image layer
  238.     App.Do( Environment, 'SelectLayer', {
  239.             'Path': (1,1,[],App.Constants.Boolean.false), 
  240.             'GeneralSettings': {
  241.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  242.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  243.                 }
  244.             })
  245.  
  246.     # place a drop shadow.  Make it a little bit more on the bottom than on the right
  247.     # we put the drop shadow on a new layer so that the image layer remains untouched
  248.     # drop shadow amount is 10 on the right and 12 on the bottom
  249.     App.Do( Environment, 'DropShadow', {
  250.             'Blur': 5.000, 
  251.             'Color': (0,0,0), 
  252.             'Horizontal': 10, 
  253.             'Vertical': 12, 
  254.             'NewLayer': App.Constants.Boolean.true, 
  255.             'Opacity': 60, 
  256.             'GeneralSettings': {
  257.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  258.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  259.                 }
  260.             })
  261.  
  262.     # The default name for a drop shadow is kind of lame.  Lets rename it
  263.     App.Do( Environment, 'LayerProperties', {
  264.             'General': {
  265.                 'Name': PSPUtils.DropShadowLayerName,
  266.                 'Opacity': 75,
  267.                 }, 
  268.             'GeneralSettings': {
  269.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  270.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  271.                 }
  272.             })
  273.  
  274.  
  275.     # placing the drop shadow added a new layer, and made it active.
  276.     # let's arrange it into the group
  277.     App.Do( Environment, 'LayerArrangeMoveIn' )
  278.     
  279.     # select one layer down to get back to the backdrop
  280.     App.Do( Environment, 'SelectLayer', { 'Path': ( 0, -1, [], App.Constants.Boolean.false ) } )
  281.  
  282.     # now create a vector layer to put text on - creating the layer will make it active
  283.     # we could let the text tool create the layer for us, but this way we get to name it
  284.     App.Do( Environment, 'NewVectorLayer', {
  285.             'General': {
  286.                 'Opacity': 100, 
  287.                 'Name': PSPUtils.CaptionTextLayerName, 
  288.                 }, 
  289.             'GeneralSettings': {
  290.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  291.                 }
  292.             })
  293.  
  294.     # finally place the text.  Make it centered in the X axis, and centered in the bottom
  295.     # margin.  We make the text height 1/4 the margin amount, but never less than 12 pixels
  296.     TextSize = max( 12, int(YAmountBottom / 4))
  297.     
  298.     TextBaseLine = Target.Height - (YAmountBottom / 4)
  299.     App.Do( Environment, 'TextEx', {
  300.             'Visibility': True, 
  301.             'CreateAs': App.Constants.CreateAs.Vector, 
  302.             'Fill': {
  303.                 'Color': (0,0,0), 
  304.                 'Pattern': None, 
  305.                 'Gradient': None, 
  306.                 'Texture': None
  307.                 }, 
  308.             'Font': FontName, 
  309.             'Italic': App.Constants.Boolean.true, 
  310.             'Stroke': None,
  311.             'PointSize': TextSize, 
  312.             'Start': (Target.Width / 2, TextBaseLine), 
  313.             'LineStyle': None,
  314.             'LineWidth':0,
  315.             'SetText': App.Constants.Justify.Center,
  316.             'TextFlow': App.Constants.TextFlow.HorizontalDown, 
  317.             'TextType': App.Constants.TextType.TextBase, 
  318.             'AntialiasStyle': App.Constants.AntialiasEx.Smooth, 
  319.             'Characters': unicode(CaptionText),
  320.             'GeneralSettings': {
  321.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  322.                 'AutoActionMode': App.Constants.AutoActionMode.Match,
  323.                 'Version': ((9,0,0),1)
  324.                 }
  325.             })
  326.  
  327.     # finish with the image layer selected
  328.     App.Do( Environment, 'SelectLayer', {
  329.             'Path': (1,1,[],App.Constants.Boolean.false), 
  330.             'GeneralSettings': {
  331.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  332.                 'AutoActionMode': App.Constants.AutoActionMode.Default
  333.                 }
  334.             })
  335.  
  336.